package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysAttendanceRecordMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysCourseMapper;

/**
 * 课程管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class SysCourseServiceImpl extends ServiceImpl<SysCourseMapper,SysCourse> implements ISysCourseService
{
    @Autowired
    private SysCourseMapper sysCourseMapper;

    @Autowired
    private ISysCourseVideoService videoService ;

    @Autowired
    private ISysCourseContentService contentService ;

    @Autowired
    private ISysHomeworkExercisesService homeworkExercisesService ;

    @Autowired
    private ISysHomeworkService homeworkService ;

    @Autowired
    private ISysAttendanceRecordService attendanceRecordService ;

    @Autowired
    private ISysTeacherService teacherService ;

    /**
     * 查询课程管理
     * 
     * @param id 课程管理主键
     * @return 课程管理
     */
    @Override
    public SysCourse selectSysCourseById(Long id)
    {
        return sysCourseMapper.selectSysCourseById(id);
    }

    /**
     * 查询课程管理列表
     * 
     * @param sysCourse 课程管理
     * @return 课程管理
     */
    @Override
    public List<SysCourse> selectSysCourseList(SysCourse sysCourse)
    {
        if (SecurityUtils.hasRole("teacher")) {
            LambdaQueryWrapper<SysTeacher> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysTeacher::getUserId ,SecurityUtils.getUserId()) ;
            SysTeacher one = teacherService.getOne(wrapper);
            if (ObjectUtils.isNotEmpty(one)) {
                sysCourse.setTeacherId(one.getId());
            }
        }
        List<SysCourse> list = sysCourseMapper.selectSysCourseList(sysCourse);
        for (SysCourse course : list) {
            LambdaQueryWrapper<SysCourseContent> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysCourseContent::getCourseId ,course.getId()) ;
            course.setCourseContent(contentService.getOne(wrapper));

            LambdaQueryWrapper<SysCourseVideo> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SysCourseVideo::getCourseId ,course.getId()) ;
            course.setCourseVideo(videoService.getOne(wrapper1));

            LambdaQueryWrapper<SysHomeworkExercises> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(SysHomeworkExercises::getCourseId ,course.getId()) ;
            course.setHomeworkExercises(homeworkExercisesService.getOne(wrapper2));


            LambdaQueryWrapper<SysHomework> wrapper3 = new LambdaQueryWrapper<>();
            wrapper3.eq(SysHomework::getCourseId ,course.getId()) ;
            course.setHomework(homeworkService.getOne(wrapper3));

            LambdaQueryWrapper<SysAttendanceRecord> wrapper4 = new LambdaQueryWrapper<>();
            wrapper4.eq(SysAttendanceRecord::getCourseId ,course.getId()) ;
            SysAttendanceRecord one = attendanceRecordService.getOne(wrapper4);
            course.setAttendanceRecord(one);
            course.setAttendance(ObjectUtils.isNotEmpty(one));

        }
        return list;
    }

    /**
     * 新增课程管理
     * 
     * @param sysCourse 课程管理
     * @return 结果
     */
    @Override
    public int insertSysCourse(SysCourse sysCourse)
    {
        sysCourse.setCreateTime(DateUtils.getNowDate());
        LambdaQueryWrapper<SysTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysTeacher::getUserId ,SecurityUtils.getUserId()) ;
        SysTeacher one = teacherService.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(one)) {
            sysCourse.setTeacherId(one.getId());
        }
        return sysCourseMapper.insertSysCourse(sysCourse);
    }

    /**
     * 修改课程管理
     * 
     * @param sysCourse 课程管理
     * @return 结果
     */
    @Override
    public int updateSysCourse(SysCourse sysCourse)
    {
        sysCourse.setUpdateTime(DateUtils.getNowDate());
        int i = sysCourseMapper.updateSysCourse(sysCourse);
        if (ObjectUtils.isEmpty(sysCourse.getParentId())) {
            LambdaUpdateWrapper<SysCourse> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(SysCourse::getId ,sysCourse.getId()) ;
            wrapper.set(SysCourse::getParentId ,null) ;
            this.update(wrapper) ;
        }
        return i;
    }

    /**
     * 批量删除课程管理
     * 
     * @param ids 需要删除的课程管理主键
     * @return 结果
     */
    @Override
    public int deleteSysCourseByIds(Long[] ids)
    {
        return sysCourseMapper.deleteSysCourseByIds(ids);
    }

    /**
     * 删除课程管理信息
     * 
     * @param id 课程管理主键
     * @return 结果
     */
    @Override
    public int deleteSysCourseById(Long id)
    {
        return sysCourseMapper.deleteSysCourseById(id);
    }
}
